package com.huanying.goodjob.publishactivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;

@Service
public class PublishactivityServiceImpl implements PublishactivityService {

	@Autowired
	private BaseDao dao;
	@Override
	public void add_activity(Publishactivity publishactivity) throws Exception {
		// TODO Auto-generated method stub
		dao.save(publishactivity);
	}
	@Override
	public void update_activity(Publishactivity publishactivity) throws Exception {
		// TODO Auto-generated method stub
		dao.update(publishactivity);
		
	}
	
	@Override
	public PageBean search_activity(Map<String,String> map, int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer("from Publishactivity as p where 1=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("title")!=null && map.get("title")!="")
		{
			hql.append(" and p.title like ?");
			param.add("%"+map.get("title")+"%");
		}
		if(map.get("context")!=null&& map.get("context")!="")
		{
			hql.append(" and p.context like ?");
			param.add("%"+map.get("context")+"%");
		}
		if(map.get("day")!=null && map.get("day")!="")
		{
			hql.append(" and TIMESTAMPDIFF(DAY,DATE_FORMAT(p.create_time,'%Y-%m-%d'),DATE_FORMAT(NOW(),'%Y-%m-%d'))<=? ");
			param.add(Integer.parseInt((String) map.get("day")));
		}
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;

	}
	@Override
	public void delete_activity(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Publishactivity set status=0 where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
	    dao.exculeByParams(hql, param);
		
	}
	@Override
	public Publishactivity get_activity(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="from Publishactivity where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
		return (Publishactivity)dao.exculeHqlGetOjbectByParams(hql, param);
	}
	@Override
	public List front_get_activitys() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Publishactivity where status=1";
		List<Object> param=new ArrayList<Object>();
		return dao.exculeHqlByParams(hql, param);
	}

}
